package com.example.customview.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.graphics.toColorInt
import com.sample.base.utils.dp
import java.util.*

class ColoredTextView(context: Context?, attrs: AttributeSet?) : AppCompatTextView(context, attrs) {

    private val random = Random()
    private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        setTextColor(Color.WHITE)
        color = COLORS[random.nextInt(COLORS.size)]


    }
    init {
        textSize = SIZE[random.nextInt(SIZE.size)].toFloat()
        setPadding(X_PADDING, Y_PADDING, X_PADDING, Y_PADDING)
    }

    override fun onDraw(canvas: Canvas) {
        canvas.drawRoundRect(
            0f,
            0f,
            width.toFloat(),
            height.toFloat(),
            ROUND_RADIUS.toFloat(),
            ROUND_RADIUS.toFloat(),
            paint
        )
        super.onDraw(canvas)
    }


    companion object {
        private val COLORS = intArrayOf(
            "#6200EE".toColorInt(),
            "#03DAC5".toColorInt(),
            "#f86b1d".toColorInt(),
            "#55bb8a".toColorInt(),
            "#b2b2b2".toColorInt(),
            "#35333c".toColorInt()
        )
        private val SIZE = intArrayOf(
            12, 26, 18
        )

        private val X_PADDING = 16.dp
        private val Y_PADDING = 6.dp
        private val ROUND_RADIUS = 6.dp

    }
}